package com.example.childrendapp.login;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

import com.example.childrendapp.Main;
import com.example.childrendapp.R;
import com.example.childrendapp.database.DataBaseHelper;
import com.example.childrendapp.menu.AppMenuChoice;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class Login extends Activity implements OnClickListener {
	private static final int TEST = 1;
	private static final String TAG = "Login";
	private DataBaseHelper mDB;
	private TextView mError;
	private TextView mLinkRegister;
	private EditText mName, mPass;
	private Button mLogin;
	private SQLiteDatabase myDataBase;
	private String name, pass;
	private LocalService mLocalSevice;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_app_login);
		mDB = new DataBaseHelper(this);
		mError = (TextView) findViewById(R.id.txtError);
		mLinkRegister = (TextView) findViewById(R.id.txtLink_Register);
		mName = (EditText) findViewById(R.id.txtUser);
		mPass = (EditText) findViewById(R.id.txtPass);
		mLogin = (Button) findViewById(R.id.btnLogin);
		mLogin.setOnClickListener(this);
		mLinkRegister.setOnClickListener(this);
		name = mName.getText().toString();
		pass = mPass.getText().toString();

		try {
			mDB.createDataBase();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mDB.openDataBase();
		try {
			mDB.copyDataBase();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		myDataBase = mDB.getWritableDatabase();

		bindService(new Intent(Login.this, LocalService.class),
				mConnection, Context.BIND_AUTO_CREATE);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated methmCod stub
		super.onResume();
		Log.i(TAG, "onResume");
	}

	public void onClick(View v) {
		

		if (v == mLogin) {
			mLocalSevice.startMp3Player();
			Cursor mCursor = myDataBase
					.rawQuery(
							"SELECT * FROM USER WHERE UserName = '"+ mName.getText().toString()+"' and PassWord = '"+ mPass.getText().toString() + "'",
							null);
			int i = mCursor.getCount();
			if (i > 0) {
				Intent data = new Intent(this, AppMenuChoice.class);
				startActivity(data);
				
				
			
			} else {
				mError.setText("Invalid, Enter Again");
				mName.setText("");
				mPass.setText("");
				mName.requestFocus();
			}
		}

		else if (v == mLinkRegister) {

			Intent intent = new Intent(this, Register.class);
			this.startActivityForResult(intent, TEST);

		}

	}
	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName name, IBinder service) {
			mLocalSevice = ((LocalService.LocalBinder) service).getService();
			
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mLocalSevice = null;
			
		}
	};
	

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == RESULT_OK) {
			name = data.getExtras().getString("NAME");
			pass = data.getExtras().getString("PASS");
			mName.setText(name);
			mPass.setText(pass);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	/*
	 * public boolean check(){ for (int j = 0; j <5; j++) { if
	 * (ArrName[j].toString().trim().equals(name) &&
	 * ArrPass[j].toString().trim().equals(pass) ) {
	 * 
	 * return true; }
	 * 
	 * } return false; }
	 */
	@Override
	protected void onPause() {
		super.onPause();
		Log.i(TAG, "onPause");
	}

	@Override
	protected void onStop() {
		/*mLocalSevice.mp3Stop();
		mLocalSevice.onDestroy();
		super.onDestroy();*/
		super.onStop();
		Log.i(TAG, "onStop");
	}
}
